package com.api.regression.group.deletedocument;

import java.io.File;
import java.io.FileInputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DocumentApi;
import com.zyncro.api.model.DocumentOrderFieldFilterApiType;
import com.zyncro.api.model.ObjectResultApi;
import com.zyncro.api.model.OrderFilterApiType;
import com.zyncro.api.model.PagedItemsResultApi;
import com.zyncro.api.model.UploadDocumentResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class DeleteDocument extends AbstractApiTest {

	@Value("#{editgroupdocuments['user.editor'].appId}")
	private String userUrn;

	@Value("#{editgroupdocuments['user.editor'].password}")
	private String userPassword;

	@Value("#{editgroupdocuments['group.toDelete'].urn}")
	private String groupUrn;

	private String folderUrn;

	private String fileUrn;

	private String linkUrn;

	private String shortcutUrn;

	@SuppressWarnings("deprecation")
	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);

		File file = new File("pom.xml");

		Long length = file.length();
		String fileName = file.getName();

		FileInputStream inputStream = new FileInputStream(file);

		ObjectResultApi<UploadDocumentResultApi> result = getApi().getService(IGroupApiService.class).uploadNewDocument(groupUrn,
				getApi().getAccessKey(), null, null, null, null, null, fileName, length, inputStream);
		this.fileUrn = result.getObject().getDocumentUrn();

		ObjectResultApi<String> folder = getApi().getService(IGroupApiService.class).createFolder(groupUrn, "Folder", null);
		this.folderUrn = folder.getObject();

		ObjectResultApi<String> link = getApi().getService(IGroupApiService.class).createExternalLink(groupUrn, "clarin",
				"http://www.clarin.com", null);
		this.linkUrn = link.getObject();

		ObjectResultApi<String> shortcut = getApi().getService(IGroupApiService.class).createInternaLink(groupUrn, fileUrn);
		this.shortcutUrn = shortcut.getObject();
	}

	@Test
	public void testDeleteFolder() throws ZyncroApiException {

		getApi().getService(IGroupApiService.class).deleteDocument(groupUrn, folderUrn);
	}

	@Test
	public void testDeleteFile() throws ZyncroApiException {

		getApi().getService(IGroupApiService.class).deleteDocument(groupUrn, fileUrn);
	}

	@Test
	public void testDeleteLink() throws ZyncroApiException {

		getApi().getService(IGroupApiService.class).deleteDocument(groupUrn, linkUrn);
	}

	@Test
	public void testDeleteShortcut() throws ZyncroApiException {

		getApi().getService(IGroupApiService.class).deleteDocument(groupUrn, shortcutUrn);
	}

	@Test(dependsOnMethods = { "testDeleteFolder", "testDeleteFile", "testDeleteLink", "testDeleteShortcut" })
	public void testGetDeletedDocuments() throws ZyncroApiException {

		OrderFilterApiType orderFilterType = null;
		DocumentOrderFieldFilterApiType orderFilterField = null;

		PagedItemsResultApi<DocumentApi> documents = getApi().getService(IGroupApiService.class).getDocuments(groupUrn, 0, 0,
				orderFilterField, orderFilterType);

		Assert.assertEquals(documents.getTotalItems(), 0, "Deleted documents");
	}
}
